/*
 * @Author: 杨三炮
 * @Date: 2021-11-18 20:05:33
 * @LastEditors: 杨三炮
 * @LastEditTime: 2021-11-23 12:20:27
 * @FilePath: \node\node\model\users_mod.js
 */

module.exports = class user_mod extends require('./model') {
    /**
     * @description: 数据库登录
     * @param {*} username
     * @param {*} password
     * @param {*} type
     * @return {*}
     */
    static loginUser (username, password, type) {

        return new Promise((resolve, reject) => {
            let sql = 'SELECT *FROM student WHERE BINARY username=? AND `password`=? AND type=?;'
            let params = [username, password, type]
            this.query(sql, params).then(result => {
                resolve(result)
            }).catch(err => {
                reject('登录失败', err)
            })
        })
    }


    /**
     * @description: 
     * @param {*} type  1 管理员 2学生 3老师
     * @param {*} currPage  第几页
     * @param {*} pageNum   每页多少个
     * @return {*}
     */
    static getUserMod () {
        return new Promise((resolve, reject) => {
            let sql = `select *from student as s
            INNER JOIN class as c
            ON s.class_id=c.c_id
            order by modifytime desc ;`
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 传入身份 查询这个身份的所有人数
     * @param {*} type 1 管理员 2学生 3老师
     * @return {*} result
     */
    static getUserByTypeTotal () {
        return new Promise((resolve, reject) => {
            let sql = `select count(0) as count from student `
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }
    /**
     * @description: 根据id查找用户 没有找到返回0
     * @param {*} u_id
     * @return {*}Promise 
     */
    static searchUserMod (u_id) {
        return new Promise((resolve, reject) => {
            let sql = `select *from student where id='${u_id}'`
            this.query(sql).then(result => {
                if (!result.length) {
                    resolve(0)
                    return
                }
                resolve(result[0])
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 根据用户id删除用户 删除成功返回1
     * @param {*} u_id
     * @return {*}返回1
     */
    static delUserdataMod (u_id) {
        return new Promise((resolve, reject) => {
            let sql = `DELETE FROM student WHERE id=${u_id};`
            this.query(sql).then(result => {
                resolve({
                    msg: '删除成功',
                    code: 200
                })
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 用户信息修改
     * @param {*} data
     * @return {*}
     */
    static upUserdataMod (data) {
        return new Promise((resolve, reject) => {
            let sql = `UPDATE student SET username = ?,sex=?,address=?,class_id=?,modifytime=NOW() WHERE id = ?;`
            let params = [data.username, data.sex, data.address, data.class_id, data.u_id]
            this.query(sql, params).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }


    /**
     * @description: 插入用户
     * @param {*} data
     * @return {*}
     */
    static setXlsxDataMod (data) {
        return new Promise((resolve, reject) => {
            let sql = `INSERT INTO student VALUES(?,?,?,NULL,NULL,?,?,NOW(),NOW(),?);`
            let params = [data.u_id, data.username, data.password, data.address, data.sex, data.class_id]
            this.query(sql, params).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 修改用户密码
     * @param {*} u_id
     * @param {*} oldPwd
     * @param {*} newPwd
     * @return {*}
     */
    static upPwdMod (u_id, oldPwd, newPwd, user) {
        if (user.password != oldPwd) {
            return { code: 201, msg: '旧密码不匹配' }
        }
        return new Promise((resolve, reject) => {
            let sql = "UPDATE student SET `password`=? WHERE id=?"
            this.query(sql, this.formatParams(newPwd, u_id)).then(result => {
                resolve({
                    code: 200,
                    msg: '修改成功'
                })
            }).catch(err => {
                reject(err)
            })
        })
    }
    
}